* ==============================================================================
* Data: experimental data 
* Project: Multiple Switching and Data Quality in the Multiple Price List
*    
*
* Last updated: September 2019
* ==============================================================================

********************************************************************************
*** Replicates all tables and figures in the paper
*** "bootstrap.do" must be run first
********************************************************************************

clear all
set more off
cap log close

global path "//your/path/"
cd "$path"
use "data.dta"
do "data_cleaning_and_recoding.do"

* Table 1
********************************************************************************
local vars1 = "female age childnumber familynumber traveltime medu mocc income allowance gradedum1 gradedum2 gradedum3 all_std switch"

foreach v in `vars1' {
	display  _newline "{text}{bf:`v':}"
	ttest `v', by(nudge)
}

* Table 2
********************************************************************************
tabulate mpl_fs nudge, column nofreq

* Table 3
********************************************************************************
tabulate ls nudge, column nofreq

* Table 4 & 5
********************************************************************************
*** Methods A-C
foreach var in mpl_total mpl_fs mpl_avg {
	covtest `var' ls, by(nudge)
}

*** Method D
preserve
use "test1_bs_results.dta", clear

ttest S_0 == S_1
summarize SP

ttest CorrS_0 == CorrS_1
summarize P_CORR

restore


* Table 6
********************************************************************************
*** Methods A-C
foreach var in mpl_total mpl_fs mpl_avg {
	covtest `var' ls if ls_first==1, by(nudge)
}
*** Method D
preserve
use "test_lsfirst_bs_results.dta", clear

ttest S_0 == S_1
summarize SP

restore


* Table 7
********************************************************************************
forvalues i=0/1 {
	foreach var in mpl_total mpl_fs mpl_avg {
		quietly regress `var' female if nudge==`i', vce(robust)
		estimates store t7_`var'_`i'
	}
}
	
estout t7_mpl_total_0 t7_mpl_fs_0 t7_mpl_avg_0 t7_mpl_total_1 t7_mpl_fs_1 t7_mpl_avg_1 , ///
	cells(b(star fmt(3)) se(par fmt(3))) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) stats(r2 N, fmt(4 0) labels(R-squared)) ///
	label collabels(none) varlabels(_cons Constant) numbers
	

* Figure 2
********************************************************************************
*** Calculate actual difference
covtest mpl_fs ls if ls_first==0, by(nudge)
local diff_mpl_first = r(S_1) - r(S_0)
covtest mpl_fs ls if ls_first==1, by(nudge)
local diff_ls_first = r(S_1) - r(S_0)
local diff = `diff_ls_first' - `diff_mpl_first'
display `diff'

*** Draw histogram
preserve
use "pm_order_results.dta", clear

* scheme from ssc package blindschemes
* type "ssc install blindschemes, replace all" to install
set scheme plotplain
histogram diff, width(0.5) start(-5) frequency ///
	xline(`diff', lcolor(red)) fcolor(%0) ///
	xlabel(-4(2)4 ,nogrid) ylabel(, nogrid) ///
	xtitle("Difference in Treatment Effects")
graph export "Figure 2.png", replace

summarize diff
local total = r(N)
summarize diff if diff>abs(`diff') | diff<-abs(`diff')
local larger = r(N)

display "share of placebo difference larger than `diff' is `larger'/`total' = " `larger'/`total'

restore


* Table 8
********************************************************************************
foreach var in mpl_total mpl_fs mpl_avg {
	quietly {
		regress `var' c.ls#i.nudge nudge
		test ls#0.nudge == ls#1.nudge
		estadd scalar p_diff = r(p)
		estimates store t8_`var'
	}
}

estout t8_mpl_total t8_mpl_fs t8_mpl_avg, ///
	cells(b(star fmt(3)) se(par fmt(3))) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	stats(p_diff r2 N, fmt(3 4 0) labels("p-value of H0:(1)=(2)" R-squared N)) ///
	label collabels(none) varlabels(_cons Constant) numbers ///
	mlabels("Method A" "Method B" "Method C")


* Table 9
********************************************************************************
regress mpl_fs c.ls#i.switch switch if nudge==0
test 0.switch#ls == 1.switch#ls
estadd scalar p_diff = r(p)
count if switch==0 & nudge==1
estadd scalar N_0 = r(N)
count if switch==1 & nudge==1
estadd scalar N_1 = r(N)
estimates store t9_r1

regress mpl_fs c.ls#i.switch switch if nudge==1
test 0.switch#ls == 1.switch#ls
estadd scalar p_diff = r(p)
count if switch==0 & nudge==0
estadd scalar N_0 = r(N)
count if switch==1 & nudge==0
estadd scalar N_1 = r(N)
estimates store t9_r2

estout t9_r1 t9_r2, ///
	cells(b(star fmt(3)) se(par fmt(3))) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	stats(p_diff r2 N_0 N_1, fmt(3 4 0) ///
		labels("p-value of H0:(1)=(2)" "R-squared" "N(Non-multiple Switchers)" "N(Multiple Switchers)")) ///
	label collabels(none) varlabels(_cons Constant) numbers ///
	mlabels("Control" "Treatment")

reg mpl_fs c.ls##nudge if switch==0


* Table 10
********************************************************************************
regress switch all_std if nudge==0, vce(robust)
estimates store t10_r1
regress switch all_std female childnumber i.grade i.income i.medu i.mocc if nudge==0, vce(robust)
estimates store t10_r2
regress switch all_std if nudge==1, vce(robust)
estimates store t10_r3
regress switch all_std female childnumber i.grade i.income i.medu i.mocc if nudge==1, vce(robust)
estimates store t10_r4

estout t10_r1 t10_r2 t10_r3 t10_r4 , ///
	keep(all_std) cells(b(star fmt(3)) se(par fmt(3))) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	stats(N, fmt(0)) ///
	label collabels(none) numbers ///
	indicate(Other controls = female)

tabulate nudge, summarize(switch)
	
* Table 11
********************************************************************************
regress mpl_fs c.ls#i.above_med2 above_med2 if nudge==0
test 0.above_med2#ls == 1.above_med2#ls
estadd scalar p_diff = r(p)
count if above_med2==0 & nudge==0
estadd scalar N_0 = r(N)
count if above_med2==1 & nudge==0
estadd scalar N_1 = r(N)
estimates store t11_r1

regress mpl_fs c.ls#i.above_med2 above_med2 if nudge==1
test 0.above_med2#ls == 1.above_med2#ls
estadd scalar p_diff = r(p)
count if above_med2==0 & nudge==1
estadd scalar N_0 = r(N)
count if above_med2==1 & nudge==1
estadd scalar N_1 = r(N)
estimates store t11_r2

estout t11_r1 t11_r2, ///
	cells(b(star fmt(3)) se(par fmt(3))) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	stats(p_diff r2 N_0 N_1, fmt(3 4 0) ///
		labels("p-value of H0:(1)=(2)" R-squared "N(Low)" "N(High)")) ///
	label collabels(none) varlabels(_cons Constant) numbers ///
	mlabels("Control" "Treatment")


	
* Figure A.1
********************************************************************************
* bootstrap sample from table 4 must be created first

preserve
use "test1_bs_results.dta", clear

* scheme from ssc package blindschemes
* type "ssc install bindschemes, replace all" to install
set scheme plotplain
histogram S_0, width(0.2) start(-2) frequency fcolor(%0) ///
	xtitle("Cov(MPL, LS)") xlabel( ,nogrid) ylabel(, nogrid) ///
	xlabel(-1.5(0.5)1.5)
graph export "Figure A_1.png", replace

restore



	
* Table A.1
********************************************************************************
local vars1 = "female age childnumber familynumber traveltime medu mocc income allowance gradedum1 gradedum2 gradedum3 all_std"

foreach v in `vars1' {
	display  _newline "{text}{bf:`v':}"
	tabulate nudge ls_first, summarize(`v') nofreq
	anova `v' ls_first##nudge
}


* Table A.2
********************************************************************************
tabulate mpl_fs nudge if switch==0, column nofreq


* Table A.3
********************************************************************************
tabstat mpl_total mpl_fs mpl_avg ls if nudge==0, stat(var)
tabstat mpl_total mpl_fs mpl_avg ls if nudge==1, stat(var)

foreach var in mpl_total mpl_fs mpl_avg ls {
	sdtest `var', by(nudge)
}

* Table A.4
********************************************************************************
foreach score in math_std verbal_std {
	regress switch `score' if nudge==0, vce(robust)
	estimates store ta4_`score'_no
	regress switch `score' female income medu mocc childnumber i.grade if nudge==0, vce(robust)
	estimates store ta4_`score'_yes
}

estout ta4_math_std_no ta4_math_std_yes ta4_verbal_std_no ta4_verbal_std_yes, ///
	keep(math_std verbal_std) cells(b(star fmt(3)) se(par fmt(3))) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	stats(N, fmt(0)) ///
	label collabels(none) numbers ///
	indicate(Other controls = female)


* Table A.5
********************************************************************************
foreach score in math_std verbal_std {
	regress switch `score' if nudge==1, vce(robust)
	estimates store ta5_`score'_no
	regress switch `score' female income medu mocc childnumber i.grade if nudge==1, vce(robust)
	estimates store ta5_`score'_yes
}

estout ta5_math_std_no ta5_math_std_yes ta5_verbal_std_no ta5_verbal_std_yes, ///
	keep(math_std verbal_std) cells(b(star fmt(3)) se(par fmt(3))) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	stats(N, fmt(0)) ///
	label collabels(none) numbers ///
	indicate(Other controls = female)

* Table A.6
********************************************************************************
*** Methods A-C
foreach var in mpl_total mpl_fs mpl_avg {
	covtest `var' ls if above_med==1, by(nudge)
}
*** Method D
preserve
use "abovemedian_bs_results.dta", clear

ttest S_0 == S_1
summarize SP

restore

